package Q1;

//集合所有接口并输出相应的回答

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.List;

public class StudentInfo implements  Add, Remove, Update, Find {
    private static List<Student> students  = new ArrayList<>();
    private static StudentInfo studentInfo= new StudentInfo();

    private StudentInfo() {
    }
    public static StudentInfo getInstance() {
        return studentInfo;
    }
    public static List<Student> getStudents() {
        return students;
    }

    private Student getStudentFromAdmin() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学生信息");
        System.out.print("学号");
        int id = Integer.parseInt(sc.next().strip());
        System.out.print("姓名");
        String name = sc.next().strip();
        System.out.print("年龄");
        int age = Integer.parseInt(sc.next().strip());
        Student student = new Student();
        try {
            student.setId(id);
            student.setAge(age);
        } catch (AgeException | IdException e) {
            System.out.println("错误"+ e.getMessage());
            return null;
        }
        student.setName(name);
        return student;
    }

    private Student getIdFromAdmin(){
        Scanner sc = new Scanner(System.in);

        System.out.println("请输入学生学号");
        System.out.print("学号：");
        int id = Integer.parseInt(sc.next().strip());

        Student student = new Student();
        try {
        student.setId(id);
        } catch (IdException e) {
            System.out.println("错误"+ e.getMessage());
            return null;
        }
        return student;
    }

    public void list() {
        if (students.isEmpty()) {
            System.out.println("暂无学生信息");
        } else {
            for (Student student : students) {
                System.out.println(student);
            }
        }
    }

    @Override
    public void add() {
        if (Add.add(getStudents(), getStudentFromAdmin())) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败，学号已存在");
        }
    }

    @Override
    public void remove() {
        if (Remove.remove(getStudents(), getStudentFromAdmin())) {
            System.out.println("删除成功");
        } else {
            System.out.println("删除失败，学号不存在");
        }
    }

    @Override
    public void update() {
        if (Update.update(getStudents(), getStudentFromAdmin())) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败，学号不存在");
        }
    }

    @Override
    public void find() {
        Student student = Find.find(getStudents(), getIdFromAdmin());
        if (student == null) {
            System.out.println("查找失败，学号不存在");
        } else {
            System.out.println(student);
        }
    }

/*//查看是否有这个文件
    public void readStudent() {
        File file = new File (filePath);
        if (file.exists()) {
            ObjectInputStream ois = null;
            try {
                ois = new ObjectInputStream(new FileInputStream(file));
                students = (ArrayList<Student>) ois.readObject();
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            } finally {
                if (ois != null) {
                    try {
                        ois.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }*/



/*    //把文件保存到本地
    public void saveStudents(){
        File file = new File(filePath);
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream(file));
            oos.writeObject(students);
            oos.flush();
            System.out.println("文件已保存");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (oos != null) {
                    oos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
 */


    public void saveStudents(){
        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new FileWriter("D:/test.txt"));
                bw.write(String.valueOf(students));
                bw.flush();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bw != null) {
                    bw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
